const SFCScriptBlock = require('@vue/compiler-sfc')
const SFCDescriptor = require('@vue/compiler-sfc')

/**
 * 解析Vue.Props
 * @param {SFCScriptBlock} scriptBlock vue解析后的Script块
 * @param {'props', 'emits'} type vue格式化后的代码
 * @return {
 * {[string]: {
 *     type: string,
 *     default: string,
 *     desc: string,
 *     required: boolean
 * }} | null}
 */
function getScriptNode(scriptBlock, type) {
    if (scriptBlock.scriptAst) {
        scriptContent = descriptor.script.content
        // 声名式
        const exportDefaultNode = scriptBlock.scriptAst.find(item => item.type === 'ExportDefaultDeclaration')
        if (exportDefaultNode) {
            properties = exportDefaultNode.declaration.properties
        }
    } else if (scriptBlock.scriptSetupAst) {
        scriptContent = descriptor.scriptSetup.content
        // setup形式
        for (let scriptSetupAstElement of scriptBlock.scriptSetupAst) {
            if (scriptSetupAstElement.type === 'VariableDeclaration') {
                // props 可能会通过变量赋值
                const value = scriptSetupAstElement.declarations[0].init
                if (value.callee && value.callee.name === 'defineProps') {
                    properties = value.arguments[0].properties
                }
            } else if (scriptSetupAstElement.type === 'ExpressionStatement') {
                // 没有变量的情况
                if (scriptSetupAstElement.expression.callee && scriptSetupAstElement.expression.callee.name === 'defineProps') {
                    properties = scriptSetupAstElement.expression.arguments[0].properties
                }
            }
        }
    }
}
